package org.jeecg.modules.online.cgform.converter.channel;

import org.apache.commons.lang.StringUtil;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.converter.FieldCommentConverter;

import java.util.List;
import java.util.Map;

/**
 * 配置型值转换
 * 只能通过配置在获取值的时候转换数据
 */
public class ConfigConvert implements FieldCommentConverter {

    protected ISysBaseAPI sysBaseAPI;

    protected String field;

    protected String table;

    protected String code;

    protected String text;

    public ConfigConvert(){
        sysBaseAPI = SpringContextUtils.getBean(ISysBaseAPI.class);
    }

    public ConfigConvert(String table,String code,String text){
        this();
        this.table = table;
        this.code = code;
        this.text = text;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }


    @Override
    public String converterToVal(String txt) {
        if(StringUtil.isNotEmpty(txt)){
            String filterSql = text + "= '" + txt + "'";
            String tableName = null;
            int index = table.indexOf("where");
            if(index>0){
                tableName = table.substring(0,index).trim();
                filterSql += " and "+ table.substring(index+5);
            }else{
                tableName = table;
            }
            List<DictModel> dictList = sysBaseAPI.queryFilterTableDictInfo(tableName,text,code,filterSql);
            if(dictList!=null && dictList.size()>0){
                return dictList.get(0).getValue();
            }
        }
        return null;
    }

    @Override
    public String converterToTxt(String val) {
        if(StringUtil.isNotEmpty(val)){
            String filterSql = code + "= '" + val + "'";
            String tableName = null;
            int index = table.indexOf("where");
            if(index>0){
                tableName = table.substring(0,index).trim();
                filterSql += " and "+ table.substring(index+5);
            }else{
                tableName = table;
            }
            List<DictModel> dictList = sysBaseAPI.queryFilterTableDictInfo(tableName,text,code,filterSql);
            if(dictList!=null && dictList.size()>0){
                return dictList.get(0).getText();
            }
        }
        return null;
    }

    @Override
    public Map<String, String> getConfig() {
        return null;
    }
}
